package com.wondertek.poms.service.config;

import com.wondertek.poms.common.constant.Constants;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.Queue;

/**
 * 商品包发能力
 *
 * @author evan0213
 */
@Configuration
@EnableJms
public class ActiveMQConfig {
	@Value("${spring.activemq.user}")
	private String usrName;

	@Value("${spring.activemq.password}")
	private String password;

	@Value("${spring.activemq.broker-url}")
	private String brokerUrl;
	@Autowired
	private ActiveMQConverter activeMQConverter;

	@Bean
	public Queue pomsTreeQueue() {
		return new ActiveMQQueue(Constants.QUEUE_POMS_TREE);
	}

	@Bean
	public ActiveMQConnectionFactory connectionFactory() {
		return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
	}

    /*@Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setPubSubDomain(false);
        factory.setConnectionFactory(connectionFactory);
        // 开启持久化
        // factory.setSubscriptionDurable(true);
        return factory;
    }*/

	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		//设置为发布订阅方式, 默认情况下使用的生产消费者方式
		bean.setPubSubDomain(true);
		bean.setConnectionFactory(connectionFactory);
		bean.setMessageConverter(activeMQConverter);
		return bean;
	}
}